<?php
// +----------------------------------------------------------------------
// | Scallion 
// +----------------------------------------------------------------------
// | Copyright (c) 2018 http://www.scallion.top All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: Liuxg < 420695648@qq.com>
// +----------------------------------------------------------------------
namespace app\admin\controller;

use cmf\controller\AdminBaseController;
use think\Db;

class ClassroomController extends AdminBaseController
{

    /**
     * 校区管理
     */
    public function campus()
    {
		$result = db('campus')->order('id asc')->paginate(10);
		$this->assign('page', $result->render());
		$this->assign("campus_info", $result);
		return $this->fetch('campus');
        return $this->fetch();
    }
	
	/**
     * 校区查询
     */
	public function find_campus(){
		if($this->request->param()){
			$param = $this->request->param();
			$result = db('campus')->where('name','like','%'.$param['name'].'%')->order('id asc')->paginate(10);
			$this->assign('page', $result->render());
			$this->assign("campus_info", $result);
			$this->assign("param", $param);
			return $this->fetch('campus');
		}
		else{
			$this->error('非法访问！');
		}
	}
	
	/**
     * 新增/编辑校区
     */
	public function add_campus(){
		return $this->fetch();
	}
	
	public function add_campus_Post(){
		if ($this->request->isPost()){
			$param = $this->request->param();
			//校区数据整理
			$param['create_time'] = time();
			$param['edit_time'] = time();
			$param['create_id'] = cmf_get_current_admin_id();
			$param['location'] = json_encode(array("lng"=>$param['lng'],"lat"=>$param['lat']));
			unset($param['location_view']);
			unset($param['lng']);
			unset($param['lat']);
			$res = db('campus')->insert($param);
			if($res){
				
				// 填写日志
				$action = '添加校区';
				$json_data = json_encode($param);
				$result = '成功';
				self::data_log($action,$json_data,$result);
				
				$this->success('添加校区成功');
			}
			else{
				$this->error('系统错误！errcode：-601');
			}
		}
		else{
			$this->error('非法访问！');
		}
	}
	
	public function edit_campus(){
		if($this->request->isGet()){
			$param = $this->request->param();
			$result = db('campus')->where('id='.$param['id'])->find();
			$point = json_decode($result['location'],true);
			$result['location_view'] = $point['lng'].','.$point['lat'];
			$result['lng'] = $point['lng'];
			$result['lat'] = $point['lat'];
			$this->assign("param", $result);
			return $this->fetch();
		}
		else{
			$this->error('非法访问！');
		}
	}
	
	public function edit_campus_Post(){
		if ($this->request->param()){
			$param = $this->request->param();
			
			//校区是否存在
			$is_exist = db('campus')->where('id='.$param['id'])->find();
			if(!$is_exist){
				$this->error('校区不存在！');
			}
			//校区数据整理
			$param['edit_time'] = time();
			$param['location'] = json_encode(array("lng"=>$param['lng'],"lat"=>$param['lat']));
			unset($param['location_view']);
			unset($param['lng']);
			unset($param['lat']);
			$res = db('campus')->where('id='.$param['id'])->update($param);
			if($res){
				
				// 填写日志
				$action = '编辑校区';
				$json_data = json_encode($param);
				$result = '成功';
				$json_old_data = json_encode($is_exist);
				self::data_log($action,$json_data,$result,$json_old_data);
				
				$this->success('编辑校区成功');
			}
			else{
				$this->error('系统错误！errcode：-602');
			}
		}
		else{
			$this->error('非法访问！');
		}
	}
	
	/**
     * 教室管理
     */
    public function classroom()
    {
		$campus = db('campus')->select();
		$campus_tree = '';
		foreach($campus as $val){
			$campus_tree = $campus_tree.'<option value="'.$val['id'].'">'.$val['name'].'</option>';
		}
		$this->assign("campus_tree",$campus_tree);
        return $this->fetch();
    }
	
	/**
     * 教室查询
     */
	public function find_classroom(){
		if($this->request->param()){
			$param = $this->request->param();
			if(!isset($param['name'])){$param['name'] = '';}
			if(isset($param['id'])){$map = 'campus_id='.$param['id'];}else{$map = false;$param['id']='';}
			$result = db('classroom')->where('name','like','%'.$param['name'].'%')->where($map)->order('id asc')->paginate(10);
			$campus = db('campus')->where('id='.$param['id'])->find();
			$param['campus_name'] = $campus['name'];
			$this->assign('page', $result->render());
			$this->assign("classroom_info", $result);
			$this->assign("param", $param);
			return $this->fetch('classroom');
		}
		else{
			$this->error('非法访问！');
		}
	}
	
	/**
     * 新增/编辑校教室
     */
	public function add_classroom(){
		$param = $this->request->param();
		if(!isset($param['id'])){$param['id']='1';}
		$campus = db('campus')->select();
		$campus_tree = '';
		foreach($campus as $val){
			$campus_tree = $campus_tree.'<option value="'.$val['id'].'" '.($val["id"]==$param["id"]?'selected="selected"':'').'>'.$val['name'].'</option>';
			if($val["id"]==$param["id"]){$name=$val;}
		}
		$this->assign("campus_tree",$campus_tree);
		$this->assign("campus",$name);
		$this->assign("param",$param);
		return $this->fetch();
	}
	
	public function add_classroom_post(){
		if($this->request->param()){
			$param = $this->request->param();
			//校区是否存在
			$is_exist = db('campus')->where('id='.$param['campus_id'])->find();
			if(!$is_exist){
				$this->error('校区不存在！');
			}
			//教室数据整理
			if($param['max_count'] == ''){$param['max_count']='30';}
			if($param['exam_count'] == ''){$param['exam_count']=$param['max_count'];}
			$param['create_time'] = time();
			$param['last_edit_id'] = cmf_get_current_admin_id();
			$param['last_edit_time'] = time();
			$res = db('classroom')->insert($param);
			if($res){
				
				// 填写日志
				$action = $is_exist['name'].'添加教室';
				$json_data = json_encode($param);
				$result = '成功';
				self::data_log($action,$json_data,$result);
				
			$this->success($is_exist['name'].'添加教室成功');
			}
			else{
				$this->error('系统错误！errcode：-603');
			}
		}
		else{
			$this->error('非法访问！');
		}
	}
	
	public function edit_classroom(){
		$param = $this->request->param();
		if(isset($param['id'])){
			$classroom_info = db('classroom')->where('id='.$param['id'])->find();
			$campus = db('campus')->select();
			$campus_tree = '';
			foreach($campus as $val){
				$campus_tree = $campus_tree.'<option value="'.$val['id'].'" '.($val["id"]==$param["id"]?'selected="selected"':'').'>'.$val['name'].'</option>';
				if($val["id"]==$classroom_info["campus_id"]){$name=$val;}
			}
			$this->assign("campus_tree",$campus_tree);
			$this->assign("campus",$name);
			$this->assign("param",$classroom_info);
			return $this->fetch();
		}
		else{
			$this->error('非法访问！');
		}
	}
	
	public function edit_classroom_post(){
		if($this->request->param()){
			$param = $this->request->param();
			//教室是否存在
			$is_exist_classroom = db('classroom')->where('id='.$param['id'])->find();
			if(!$is_exist_classroom){
				$this->error('教室不存在！');
			}
			//校区是否存在
			$is_exist_campus = db('campus')->where('id='.$param['campus_id'])->find();
			if(!$is_exist_campus){
				$this->error('校区不存在！');
			}
			//教室数据整理
			if($param['max_count'] == ''){$param['max_count']='30';}
			if($param['exam_count'] == ''){$param['exam_count']=$param['max_count'];}
			$param['last_edit_id'] = cmf_get_current_admin_id();
			$param['last_edit_time'] = time();
			$res = db('classroom')->where('id='.$param['id'])->update($param);
			if($res){
				
				// 填写日志
				$action = $is_exist_campus['name'].'编辑教室';
				$json_data = json_encode($param);
				$result = '成功';
				$json_old_data = json_encode($is_exist_classroom);
				self::data_log($action,$json_data,$result,$json_old_data);
				
			$this->success($is_exist_classroom['name'].'教室编辑成功');
			}
			else{
				$this->error('系统错误！errcode：-604');
			}
		}
		else{
			$this->error('非法访问！');
		}
	}
}
